/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CronoGUI.java
 *
 * Created on 06-abr-2011, 22:59:55
 */
package rfid;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import org.llrp.ltk.generated.enumerations.GetReaderConfigRequestedData;
import org.llrp.ltk.generated.messages.GET_READER_CONFIG;
import org.llrp.ltk.generated.messages.GET_READER_CONFIG_RESPONSE;
import org.llrp.ltk.types.UnsignedShort;

/**
 *
 * @author xandru
 */
public class CronoGUI extends javax.swing.JFrame implements RFIDEventListener{

    private LTKInventory app;
    final int CRONO_OFF = 0;
    final int CRONO_ON = 1;
    int estado = CRONO_OFF;
    private Asociador asociador;
    private Properties configuracion;
    private String path="llrp.conf";
    GestorTags gestor;

    /** Creates new form CronoGUI */
    public CronoGUI() {
        initComponents();
        jButton1.setText("Empezar");
        asociador = new Asociador(textChip);
        gestor = new GestorTags();
    }

    public Properties readConfiguration(String file) throws IOException
    {
        if (file!=null)
           path = file;
        configuracion = new Properties();
        configuracion.load(new FileInputStream(path));
        return configuracion;
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFrame1 = new javax.swing.JFrame();
        ficheroGuardar = new javax.swing.JFileChooser();
        ficheroCargar = new javax.swing.JFileChooser();
        dialogo = new javax.swing.JDialog();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        listaTags = new javax.swing.JList();
        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        borraranteriores = new javax.swing.JCheckBox();
        jPanel2 = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();
        textChip = new javax.swing.JTextField();
        textDorsal = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        asignar = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        listaAsociados = new javax.swing.JList();
        cargarAsignacion = new javax.swing.JButton();
        guardarAsignacion = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        textCrono = new javax.swing.JTextField();
        textAsociaciones = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        infoPanel = new javax.swing.JPanel();
        labelEstado = new javax.swing.JLabel();
        labelAsignacion = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        Acciones = new javax.swing.JMenu();

        jFrame1.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout dialogoLayout = new javax.swing.GroupLayout(dialogo.getContentPane());
        dialogo.getContentPane().setLayout(dialogoLayout);
        dialogoLayout.setHorizontalGroup(
            dialogoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        dialogoLayout.setVerticalGroup(
            dialogoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("CronoGUI RFID");

        jButton1.setText("Empezar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        listaTags.setModel(new DefaultListModel());
        jScrollPane1.setViewportView(listaTags);

        jLabel1.setText("Guardar en:");

        borraranteriores.setText("Borrar Resultados anteriores");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jButton1)
                .addGap(18, 18, 18)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(borraranteriores)
                .addContainerGap(200, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 622, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(borraranteriores))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 292, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Captura tiempos cronometraje", jPanel1);

        jButton2.setText("Empezar Lecturas");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        textChip.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textChipActionPerformed(evt);
            }
        });

        jLabel2.setText("Chip");

        jLabel3.setText("Dorsal");

        asignar.setText("Asignar");
        asignar.setEnabled(false);
        asignar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                asignarActionPerformed(evt);
            }
        });

        listaAsociados.setModel(new DefaultListModel());
        jScrollPane2.setViewportView(listaAsociados);

        cargarAsignacion.setText("Cargar Asignacion");
        cargarAsignacion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cargarAsignacionActionPerformed(evt);
            }
        });

        guardarAsignacion.setText("Guardar Asignacion");
        guardarAsignacion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guardarAsignacionActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addGap(165, 165, 165)
                        .addComponent(cargarAsignacion)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(guardarAsignacion)
                        .addContainerGap())
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textDorsal, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(asignar)
                        .addContainerGap())
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(textChip, javax.swing.GroupLayout.DEFAULT_SIZE, 578, Short.MAX_VALUE))))
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 622, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(guardarAsignacion)
                    .addComponent(cargarAsignacion))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(textChip, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(textDorsal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(asignar))
                .addGap(21, 21, 21)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Asociacion dorsales - chips", jPanel2);

        jLabel4.setText("Fichero cronometraje");

        jLabel5.setText("Fichero de Asociaciones");

        jButton3.setText("Seleccionar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Seleccionar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("Generar Resutados");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jButton5)
                        .addContainerGap())
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(textAsociaciones, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)
                            .addComponent(textCrono, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jButton3)
                            .addComponent(jButton4))
                        .addGap(213, 213, 213))))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(textCrono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(textAsociaciones, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton4))
                .addGap(41, 41, 41)
                .addComponent(jButton5)
                .addContainerGap(172, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Resultados", jPanel3);

        labelEstado.setText("Inactivo");

        labelAsignacion.setText("");

        javax.swing.GroupLayout infoPanelLayout = new javax.swing.GroupLayout(infoPanel);
        infoPanel.setLayout(infoPanelLayout);
        infoPanelLayout.setHorizontalGroup(
            infoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(infoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelEstado)
                .addGap(60, 60, 60)
                .addComponent(labelAsignacion)
                .addContainerGap(452, Short.MAX_VALUE))
        );
        infoPanelLayout.setVerticalGroup(
            infoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(infoPanelLayout.createSequentialGroup()
                .addGroup(infoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelEstado)
                    .addComponent(labelAsignacion))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        Acciones.setText(".");
        jMenuBar1.add(Acciones);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 627, Short.MAX_VALUE)
            .addComponent(infoPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(infoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (estado == CRONO_OFF) {

            try {
                configuracion = readConfiguration(path);
            } catch (IOException ex) {
                Logger.getLogger(CronoGUI.class.getName()).log(Level.SEVERE, null, ex);
            }

            app = new LTKInventory(configuracion);
            
            if (borraranteriores.isSelected()){
               listaTags.setModel(new DefaultListModel());
               gestor = new GestorTags();
            }
            else {
                DefaultListModel mold = (DefaultListModel) listaTags.getModel();
                listaTags.setModel(new DefaultListModel());
                DefaultListModel mnew = (DefaultListModel) listaTags.getModel();


                for (TagData t : gestor.values())
                    mnew.addElement("<->" + t.getEPCParameter());
            }
            
            try {
                gestor.addListener(new RFIDLogger());
                gestor.addListener(new RFIDBeeper());
                //gestor.addListener(new ConsoleOutput());
            } catch (FileNotFoundException ex) {
                Logger.getLogger(CronoGUI.class.getName()).log(Level.SEVERE, null, ex);
            } catch (Exception ex) {
                Logger.getLogger(CronoGUI.class.getName()).log(Level.SEVERE, null, ex);
            }
            gestor.addListener(this);
            app.setGestorTags(gestor);
            gestor.start();
            System.out.println("Starting reader.");
            app.run(configuracion.getProperty("host"));
            jButton1.setText("Parar");
            estado = CRONO_ON;
            labelEstado.setText("Conectado");
            RFIDUtils.getReaderConfig(app.getConnection(configuracion.getProperty("host")));
        } else {
            System.out.println("Stopping reader.");
            app.stop();
            try {
                app.getGestorTags().save(jTextField1.getText()+ "_lecturas");
            } catch (FileNotFoundException ex) {
                Logger.getLogger(CronoGUI.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(CronoGUI.class.getName()).log(Level.SEVERE, null, ex);
            }
            jButton1.setText("Empezar");
            estado = CRONO_OFF;
            labelEstado.setText("Inactivo");
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void textChipActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textChipActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_textChipActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        
        if (estado == CRONO_OFF) {
             try {
                configuracion = readConfiguration(path);
            } catch (IOException ex) {
                Logger.getLogger(CronoGUI.class.getName()).log(Level.SEVERE, null, ex);
            }
            app = new LTKInventory(configuracion);
            //GestorTags gestor = new GestorTags();
            //gestor.addListener(new ConsoleOutput());
            gestor.addListener(asociador);
            app.setGestorTags(gestor);
            gestor.start();
            System.out.println("Starting reader.");
            app.run(configuracion.getProperty("host"));
            jButton2.setText("Detener Lecturas");
            estado = CRONO_ON;
            labelEstado.setText("Conectado");
            cargarAsignacion.setEnabled(false);
            guardarAsignacion.setEnabled(false);
            asignar.setEnabled(true);

        } else {
            System.out.println("Stopping reader.");
            Properties p = asociador.getAsociaciones();
            System.out.println(p);
            app.stop();
            
            cargarAsignacion.setEnabled(true);
            guardarAsignacion.setEnabled(true);
            asignar.setEnabled(false);
            jButton2.setText("Empezar Lecturas");
            estado = CRONO_OFF;
            labelEstado.setText("Inactivo");
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void asignarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_asignarActionPerformed
        boolean asociado=false;
        if (textDorsal.getText().length()!=0)
            asociado = asociador.asociarDorsal(textChip,textDorsal);
        else
            asociado = false;

        if (!asociado)
            System.out.println(asociador.getError());
        else {
            DefaultListModel m = (DefaultListModel)listaAsociados.getModel();
            m.clear();

            Properties p = asociador.getAsociaciones();
            System.out.println(p);
            for(Enumeration e=p.keys();e.hasMoreElements();)
            {
                String key = (String)e.nextElement();
                String value = p.getProperty(key);
                m.addElement(value + "<->" + key );
            }
        }
    }//GEN-LAST:event_asignarActionPerformed

    private void guardarAsignacionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardarAsignacionActionPerformed
            // TODO add your handling code here:
        int resul = ficheroGuardar.showOpenDialog(this);
        File f = ficheroGuardar.getSelectedFile();
        try {
            Properties p = asociador.getAsociaciones();
            System.out.println(p);
            p.store(new FileOutputStream(f), "Asociaciones");
            System.out.println(p);
        } catch (IOException ex) {
            Logger.getLogger(CronoGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_guardarAsignacionActionPerformed

    private void cargarAsignacionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cargarAsignacionActionPerformed
        // TODO add your handling code here:
        int resul = ficheroCargar.showOpenDialog(this);
        File f = ficheroCargar.getSelectedFile();
        if (f==null)
            return;

        System.out.println(f);
        try {
            Properties p = new Properties();
            p.load(new FileInputStream(f));

            asociador.setAsociaciones(p);

            DefaultListModel m = (DefaultListModel)listaAsociados.getModel();
            m.clear();

            for(Enumeration e=p.keys();e.hasMoreElements();)
            {
                String key = (String)e.nextElement();
                String value = p.getProperty(key);
                m.addElement(value + " <-> " + key);
            }
            labelAsignacion.setText("Asignacion Cargada ->" + f.getAbsolutePath());
        } catch (IOException ex) {
            Logger.getLogger(CronoGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_cargarAsignacionActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        int resul = ficheroCargar.showOpenDialog(this);
        File f = ficheroCargar.getSelectedFile();
        if (f==null)
            return;
        textCrono.setText(f.getName());
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        int resul = ficheroCargar.showOpenDialog(this);
        File f = ficheroCargar.getSelectedFile();
        if (f==null)
            return;
        textAsociaciones.setText(f.getName());
    }//GEN-LAST:event_jButton4ActionPerformed

    private void dialogoError(String men)
    {
            dialogo.setTitle("ERROR");
            JLabel mensaje = new JLabel(men);
            mensaje.setText(men);
            dialogo.setLayout(new BorderLayout());
            dialogo.getContentPane().add(mensaje);
            dialogo.setSize(new Dimension(600,200));
            dialogo.setModal(true);
            dialogo.setVisible(true);
    }

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        if ((textCrono.getText().length()<1) || (textAsociaciones.getText().length()<1))
        {
            dialogoError(" Debe elegirse un fichero de tiempos y un fichero de asocion de dorsales");
        }
    }//GEN-LAST:event_jButton5ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                    new CronoGUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu Acciones;
    private javax.swing.JButton asignar;
    private javax.swing.JCheckBox borraranteriores;
    private javax.swing.JButton cargarAsignacion;
    private javax.swing.JDialog dialogo;
    private javax.swing.JFileChooser ficheroCargar;
    private javax.swing.JFileChooser ficheroGuardar;
    private javax.swing.JButton guardarAsignacion;
    private javax.swing.JPanel infoPanel;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JLabel labelAsignacion;
    private javax.swing.JLabel labelEstado;
    private javax.swing.JList listaAsociados;
    private javax.swing.JList listaTags;
    private javax.swing.JTextField textAsociaciones;
    private javax.swing.JTextField textChip;
    private javax.swing.JTextField textCrono;
    private javax.swing.JTextField textDorsal;
    // End of variables declaration//GEN-END:variables

    public void onNewTag(RFIDTagEvent e) {
        int pos = listaTags.getModel().getSize();

        DefaultListModel m = (DefaultListModel)listaTags.getModel();
        String tagid = e.getTagData().getEPCParameter().toString();
        String corredor = null;

        if ((asociador.getAsociaciones()!=null)&&(asociador.getAsociaciones().size()>0)) //si hay asociaciones
        {
           corredor = asociador.getAsociaciones().getProperty(tagid);
        }
        if (corredor==null)
            corredor="";
        m.addElement(corredor + "<->" + tagid);
        
    }

    public void onUpdateTag(RFIDTagEvent e) {
        DefaultListModel m = (DefaultListModel)listaTags.getModel();
        int pos = m.indexOf(e.getTagData().getEPCParameter());
    }

    private void pantallaDorsales() {
        JFrame f = new JFrame();
        f.pack();

    }



}
